package com.ray.controller;

import com.ray.bean.DatatablesViewPageBean;
import com.ray.bean.TableData;
import com.ray.exception.ServiceException;
import com.ray.protocol.constants.TimeConst;
import com.ray.service.DormManageService;
import com.ray.service.model.dorm.PersonOfDormStatisticsOut;
import com.ray.util.ExcelExportUtil;
import com.ray.util.RequestUtil;
import com.ray.util.ResponseUtil;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * 宿舍管理控制器
* <p>Title: DormManageController</p>  
* <p>Description: </p>  
* @author 李斌  
* @date 2018年4月26日
 */
@SuppressWarnings("unchecked")
@Controller
@RequestMapping("/dormManage")
public class DormManageController {
	
	@Autowired
	private DormManageService dormManageService;
	
	/**
	 * @throws ServiceException 
	 * 新增宿舍管理信息
	* @date 2018年4月26日 上午8:33:41 
	* @author 李斌
	* @return Map<String,Object> 
	* @throws
	 */
	
	@RequestMapping("/addDormManage")
	@ResponseBody
	public Map<String,Object> addDormMange(HttpServletRequest request) throws ServiceException{
		Map<String,Object> map = RequestUtil.getRequestParamByGeneric(request);
		boolean flag = dormManageService.addDormManage(map); 
		if(flag) {
			return ResponseUtil.ResponseResult("保存成功");
		}else {
			return ResponseUtil.ResponseError("保存失败");
		}
	}
	
	/**
	 * 查询宿舍信息
	* @date 2018年4月26日 下午5:23:49 
	* @author 李斌
	* @return Map<String,Object> 
	* @throws
	 */
	@RequestMapping("/getDormManageByDetail")
	@ResponseBody
	public Map<String,Object> getDormManageByDetail(HttpServletRequest request) throws ServiceException{
		Map<String,Object> map = RequestUtil.getRequestParamByGeneric(request);
		//当前页数不为空,则是分页
		if(map.get("currentPage")!=null) {
			return ResponseUtil.ResponseResult(dormManageService.getDormMangeByPage(map));
		}  
		return ResponseUtil.ResponseResult(dormManageService.getDormManageByDetail(map));
	}
	
	/**
	 * 获取电表和水表信息
	* @date 2018年4月26日 下午7:15:51 
	* @author 李斌
	* @return Map<String,Object> 
	* @throws
	 */
	@RequestMapping("/getEnergyAndWaterByDetail")
	@ResponseBody
	public Map<String,Object> getEnergyAndWaterByDetail(HttpServletRequest request) throws ServiceException{
		Map<String, Object> map = RequestUtil.getRequestParamByGeneric(request);
		return ResponseUtil.ResponseResult(dormManageService.getEnergyAndWaterByDetail(map));
	}
	 
	@RequestMapping("/getAllUserOnIdAndName")
	@ResponseBody
	public Map<String,Object> getAllUserOnIdAndName(HttpServletRequest request) throws ServiceException{
		Map<String,Object> map = RequestUtil.getRequestParamByGeneric(request);
		return ResponseUtil.ResponseResult(dormManageService.getAllUserOnIdAndName(map));
	}
	
	@RequestMapping("/getDormByDromNo")
	@ResponseBody 
	public Map<String,Object> getDormByDromNo(HttpServletRequest request) throws ServiceException{
		Map<String,Object> map = RequestUtil.getRequestParamByGeneric(request);
		return ResponseUtil.ResponseResult(dormManageService.getDormByDormNo(map));
	}
	
	/**
	 * 修改宿舍信息
	* @date 2018年4月26日 下午7:16:08 
	* @author 李斌
	* @return Map<String,Object> 
	* @throws
	 */
	@RequestMapping("/updateDormManageByDetail") 
	public Map<String,Object> updateDormManageByDetail(HttpServletRequest request) throws ServiceException{
		Map<String,Object> map = RequestUtil.getRequestParamByGeneric(request);
		dormManageService.updateDormManageByDetail(map);
		return ResponseUtil.ResponseResult("修改成功"); 
	}
	
	/**
	 * 禁用宿舍(删除宿舍)
	* @date 2018年4月30日 下午2:54:16 
	* @author 李斌
	* @return Map<String,Object> 
	* @throws
	 */
	@RequestMapping("/setDormToDisable")
	@ResponseBody
	public Map<String,Object> setDormToDisable(HttpServletRequest request) throws ServiceException{
		Map<String,Object> map = RequestUtil.getRequestParamByGeneric(request);
		dormManageService.setDormToDisable(map);
		return ResponseUtil.ResponseResult("删除成功");
	}
	
	/**
	 * @throws ServiceException 
	 * 获取宿舍用电用水汇总
	* @date 2018年4月29日 下午4:34:39 
	* @author 李斌
	* @return List<DormEWTotalPojo> 
	* @throws  
	 */
	@RequestMapping("/getDormEWTotalData")
	@ResponseBody
	public Map<String, Object> getDormEWTotalData(HttpServletRequest request) throws ServiceException{
		Map<String,Object> map = RequestUtil.getRequestParamByGeneric(request);
		return ResponseUtil.ResponseResult(dormManageService.getDormEWTotalData(map));
	}
	
	/**
	 * @throws ParseException 
	 * 宿舍个人水电费用汇总
	* @date 2018年5月1日 下午4:26:39 
	* @author 李斌
	* @return Map<String,Object> 
	* @throws
	 */
	@RequestMapping("/getPeoplesFeeForDorm")
	@ResponseBody
	public Map<String,Object> getPeoplesFeeForDorm(HttpServletRequest request) throws ServiceException, ParseException{
		Map<String,Object> map =RequestUtil.getRequestParamByGeneric(request);
		return ResponseUtil.ResponseResult(dormManageService.getPeoplesFeeForDorm(map));
	}


	/**
	 * 个人用电统计
	 * @param request
	 * @return
	 */
	@RequestMapping("/getPersonOfDormStatistics")
	@ResponseBody
	public Map<String,Object> getPersonOfDormStatistics(HttpServletRequest request){
		Map<String,String> map =RequestUtil.getRequestAllParam2String(request);
		String startTime=getFormatTimeFromMap(map,"startTime");
		String endTime=getFormatTimeFromMap(map,"endTime");
		String company=map.get("loginCompany");
		return ResponseUtil.ResponseResult(dormManageService.getPersonOfDormStatistics(startTime,endTime,company));
	}
	
	
	//下载员工达成率数据
	@RequestMapping(value="/downlodePersonOfDormStatistics",method=RequestMethod.GET)
	@ResponseBody
	public void downlodeReachFile(HttpServletRequest request,HttpServletResponse response) throws ServiceException, CloneNotSupportedException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, IOException{
		Map<String,String> map =RequestUtil.getRequestAllParam2String(request);
		String startTime=getFormatTimeFromMap(map,"startTime");
		String endTime=getFormatTimeFromMap(map,"endTime");
		String company=map.get("loginCompany");
		List<PersonOfDormStatisticsOut> tableList=dormManageService.getPersonOfDormStatistics(startTime,endTime,company);
		String [] tableHeadFields={"姓名","用电量","用水量","电费(元)","水费(元)"};
		String [] fields={"username","useTotalOfElectricity","useTotalOfWater","electricFee","waterFee"};
		String tempFolder=request.getServletContext().getRealPath("/")+"tempExcelFolder";
		File excelFile=ExcelExportUtil.createExcelFile(tableList,tempFolder,tableHeadFields,fields);
		ResponseUtil.downloadfile(response, excelFile, startTime.substring(0,7)+"个人水电统计.xlsx", true);
	}
	
	


	/**
	 * 宿舍水电统计
	 * @param request
	 * @return
	 */
    @RequestMapping("/getDormStatistics")
    @ResponseBody
    public Map<String,Object> getDormStatistics(HttpServletRequest request){
        Map<String,String> map =RequestUtil.getRequestAllParam2String(request);
        String startTime=getFormatTimeFromMap(map,"startTime");
        String endTime=getFormatTimeFromMap(map,"endTime");
        String company=map.get("loginCompany");
        return ResponseUtil.ResponseResult(dormManageService.getDormStatistics(startTime,endTime,company));

    }
	
	private String getFormatTimeFromMap( Map<String,String> map,String key){
		String value=map.get(key);
		return value==null?null:new DateTime(Long.valueOf(value)).toString(TimeConst.SECOND_FORMAT_WITH_SEPARATOR);
	}

}
